package day14;

import sun.security.provider.MD5;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//用户系统
public class AccountSystem{
    static Scanner sc=new Scanner(System.in);
    static List<Account> list=new ArrayList<>();
    public static void main(String[] args) {
        one();
    }
    public static void one(){
        while(true){
            System.out.println("***XXXXX系统***");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("***请输入编号***");
            int n=sc.nextInt();
            switch (n){
                case 1:login();break;
                case 2:reg();break;
                case 3:return;
            }
        }
    }
    //如果第一次登录成功 可以记录缓存(登录成功的记录)
    //如果下一次再登录 还是相同的账号(可以不输入密码直接免登录)
    //缓存的记录 一直存在30秒失效
    public static void login(){
        System.out.println("请输入账号");
        String account=sc.next();
        //通过缓存验证是否登录过 ==>two(?)
        Account acc=checkCookie(account);
        if(acc!=null){
            two(acc); return;
        }
        System.out.println("请输入密码");
        String password=sc.next();
        for(Account a:list){
            if(account.equals(a.username)
                    &&password.equals(a.password)){
                System.out.println("是否保存本地缓存y/n,30秒内免登录");
                String yn=sc.next();
                if("y".equals(yn)){
                    addCookie(a);
                    //场景可周期定长池  30S首次延迟功能
                }
                two(a); return;
            }
        }
    }
    public static Account checkCookie(String account){
        try(BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("d:\\io\\cookie.txt")
                )
        )){
            String msg=br.readLine();
            //将账号加密后和之前保存加密信息 匹配是否一样
            if(msg!=null && msg.equals(md5(account))){
                for(Account a:list) {
                  if(a.username.equals(account)) return a;
                }
            }
        }catch (FileNotFoundException e){
            System.out.println("首次登录");
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
    public static String md5(String msg) {
        StringBuilder hexString=null;
        try {
// 获取 MessageDigest 类的实例，指定算法为 MD5
            MessageDigest digest = MessageDigest.getInstance("MD5");
// 对字符串进行加密
            byte[] encodedHash = digest.digest(msg.getBytes());
// 将加密后的字节数组转换为十六进制字符串
            hexString = new StringBuilder();
            for (byte b : encodedHash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }
    public static void addCookie(Account a){
        try(FileOutputStream fos=
                    new FileOutputStream("d:\\io\\cookie.txt")){
            fos.write(md5(a.username).getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void reg(){
        System.out.println("请输入账号");
        String account=sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        Account a=new Account(account,password);
        //存储集合
        list.add(a);
        //做序列化
    }
    //?
    public static void two(Account a){
        System.out.println(a.username+"登录成功");
        while(true){
            System.out.println("1.聊天功能");
            System.out.println("2.退出登录");
            System.out.println("0.返回上级");
            int n=sc.nextInt();
            switch (n){
                case 1: break;
                case 2: break;
                case 0: return;
            }
        }
    }
}
class Account{
    String username;
    String password;
    public Account(String username, String password) {
        this.username = username;
        this.password = password;
    }
}
